package com.example.jetpack_workmanager

import android.graphics.drawable.Drawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
import androidx.work.*
import com.example.jetpack_workmanager.mywork.*
import java.time.Duration
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.concurrent.timerTask

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun mAddWork(view: View) {

        //设置触发条件
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.NOT_REQUIRED)
            .build()

        val inputData = Data.Builder().putString("input_data", "jack").build()

        //配置任务
        //一次性执行的任务
        val workRequest1 = OneTimeWorkRequest.Builder(MyWork::class.java)
            //设置触发条件
            .setConstraints(constraints)
            //设置延时执行
//            .setInitialDelay(5, TimeUnit.SECONDS)
            //指数退避策略
            .setBackoffCriteria(BackoffPolicy.LINEAR, Duration.ofSeconds(2))
            //设置tag标签
            .addTag("workRequest1")
            //参数传递
            .setInputData(inputData)
            .build()
        //周期性任务 时间不能少于15分钟
        val workRequest2 =
            PeriodicWorkRequest.Builder(MyWork::class.java, Duration.ofMinutes(15)).build()

        val workManager = WorkManager.getInstance(this)
        workManager.enqueue(workRequest1)


        //观察任务状态
        workManager.getWorkInfoByIdLiveData(workRequest1.id).observe(this, Observer {
            if (it != null && it.state === WorkInfo.State.SUCCEEDED) {
                val outputData = it.outputData.getString("output_data")
                Log.d("MainActivity", "outputData:${outputData}")
            }
            Log.d("MainActivity", "toString:${it.toString()}")
        })


        //取消任务
//        Timer().schedule(timerTask {
//            workManager.cancelWorkById(workRequest1.id)
//        }, 2000)

    }

    fun mAddWorks(view: View) {

//        val workA = OneTimeWorkRequest.Builder(Awork::class.java).build()
//        val workB = OneTimeWorkRequest.Builder(Bwork::class.java).build()
//
//        WorkManager.getInstance(this)
//            .beginWith(workA)
//            .then(workB)
//            .enqueue()


        val workA = OneTimeWorkRequest.Builder(Awork::class.java).build()
        val workB = OneTimeWorkRequest.Builder(Bwork::class.java).build()
        val workC = OneTimeWorkRequest.Builder(Cwork::class.java).build()
        val workD = OneTimeWorkRequest.Builder(Dwork::class.java).build()
        val workE = OneTimeWorkRequest.Builder(Ework::class.java).build()

        //任务组合
        val workContinuation1 = WorkManager.getInstance(this)
            .beginWith(workA)
            .then(workB)

        //任务组合
        val workContinuation2 = WorkManager.getInstance(this)
            .beginWith(workC)
            .then(workD)

        val workContinuationList = listOf<WorkContinuation>(workContinuation1, workContinuation2)

        WorkContinuation.combine(workContinuationList)//合并任务组合
            .then(workE)
            .enqueue()
    }
}



